package com.dykj.fireFighting.web.devicemanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.DeviceTraditionDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.DeviceTradition;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceTraditionService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 设备管理-传统设备表接口
 *
 * @Author aixiaolan
 * @Date  2021-06-21 15:25:03
 */
@Slf4j
@Api(value = "设备管理-传统设备表接口", tags = {"设备管理-传统设备表接口"})
@RestController
@RequestMapping("devicemanger/deviceTradition")
public class DeviceTraditionController {

   @DubboReference
    private IDeviceTraditionService deviceTraditionService;

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 设备管理-传统设备表列表
     *
     * @return
     */
    @AutoLog(value = "设备管理-传统设备表-分页列表查询")
    @ApiOperation(value = "设备管理-传统设备表分页列表", notes = "设备管理-传统设备表分页列表")
    @GetMapping("/list")
//    @RequiresPermissions("deviceTradition/list")
    public CommonResult<PageVo<DeviceTradition>> treeGrid(DeviceTraditionDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<DeviceTradition> page = deviceTraditionService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取设备管理-传统设备表信息
     */
    @AutoLog(value = "设备管理-传统设备表-获取设备管理-传统设备表信息")
    @ApiOperation(value = "获取设备管理-传统设备表信息", notes = "获取设备管理-传统设备表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
//    @RequiresPermissions("deviceTradition/info")
    public CommonResult<DeviceTradition> info(@PathVariable("id") String id){
        DeviceTradition deviceTradition = deviceTraditionService.getById(id);
        return CommonResult.success(deviceTradition);
    }

    /**
     * 保存设备管理-传统设备表信息
     */
    @AutoLog(value = "设备管理-传统设备表-保存设备管理-传统设备表信息")
    @ApiOperation(value = "保存设备管理-传统设备表信息", notes = "保存设备管理-传统设备表信息")
    @PostMapping("/save")
//    @RequiresPermissions("deviceTradition/save")
    public CommonResult save(@Valid @RequestBody DeviceTradition deviceTradition) {
        try {
            deviceTradition.setCreateTime(new Date());
            deviceTradition.setUpdateTime(new Date());
            deviceTradition.setCreateBy(getName());
            deviceTradition.setUnitId(getEnterpriseId());
                deviceTraditionService.save(deviceTradition);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 修改设备管理-传统设备表信息
     */
    @AutoLog(value = "设备管理-传统设备表-修改设备管理-传统设备表信息")
    @ApiOperation(value = "修改设备管理-传统设备表信息", notes = "修改设备管理-传统设备表信息")
    @PostMapping("/update")
//    @RequiresPermissions("deviceTradition/update")
    public CommonResult update(@Valid @RequestBody DeviceTradition deviceTradition) {
        try {
            deviceTradition.setUpdateTime(new Date());
                deviceTraditionService.updateById(deviceTradition);
            deviceTradition.setUpdateBy(getName());
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 删除设备管理-传统设备表信息
     */
    @AutoLog(value = "设备管理-传统设备表-删除设备管理-传统设备表信息")
    @ApiOperation(value = "删除设备管理-传统设备表信息", notes = "删除设备管理-传统设备表信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
//    @RequiresPermissions("deviceTradition/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                deviceTraditionService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 下拉列表
     * @return
     */
    @GetMapping("/list2")
    public CommonResult<List<DeviceTradition>> getList() {
        List<DeviceTradition> page = deviceTraditionService.getList(ShiroUtil.getUser().getEnterpriseId().toString());
        return CommonResult.success(page);
    }
}
